## Replication for Unilateral Powers, Public Opinion, and the Presidency by
## Andrew Reeves and Jon Rogowski Use this code to replicate columns 2,3, and 4 of
## Tables 1, 2, and 3 of the Online SI.

rm(list = ls())
library(foreign)
library(psych)
library(apsrtable)
## Agree = support of president
recodeTapsPos <- function(x){
  tmp <- rep(NA, length(x))
  tmp[x == "strongly agree"] <- 4
  tmp[x == "somewhat agree"] <- 3
  tmp[x == "somewhat disagree"] <- 2
  tmp[x == "strongly disagree"] <- 1
  return(tmp)
}
recodeTapsNeg <- function(x){
  tmp <- rep(NA, length(x))
  tmp[x == "strongly agree"] <- 1
  tmp[x == "somewhat agree"] <- 2
  tmp[x == "somewhat disagree"] <- 3
  tmp[x == "strongly disagree"] <- 4
  return(tmp)
}
flipScale <- function(x){
  tmp <- as.numeric(x)
  tmp <- recode(tmp, "2=1;3=2;4=3;5=4;6=5")
  return(tmp)
}

## Load Data
## For reference, these are all of the TAPS variables we use in our analyses.  
#tapsData <- tapsData[,c("wustlid",
#  "GIBRLAW1S30", "GIBRLAW2S30", "GIBRLAW4S30", "GIBRLAW7S30",
#  "GIBRLAW8S30", "gendersp", "test_PID7", "incomehhldsp", "RACE1SP",
#  "RACE2SP", "HISPAN1SP", "apprpressp", "DEC13_JAN14_PRESAPPR", "APPRPRESS38",
#  "IDEOL1SP", "educsp", "REEVES6S25", "REEVES7S25", "REEVES8S25", "REEVES6S30", "REEVES7S30",
#  "REEVES8S30", "REEVES6S25", "REEVES7S38", "REEVES8S38", "jan2014wt1",
#  "may2014wt1", "jan2015wt1", "POLKNOW1S30",  "POLKNOW2S30",  "POLKNOW3S30",  "POLKNOW5S30",  "POLKNOW6S30",  "POLKNOW7S30",  "POLKNOW12S30", "POLKNOW13S30",
#  "POLKNOW17S30", "POLKNOW18S30", "POLKNOW21S30", "POLKNOW22S30", "POLKNOW23S30" ,"POLKNOW24S30", "POLKNOW25S30"
#)]
##
setwd("")
load("rr_taps.Rdata")

## Rule of law
tapsData$law.scale <- alpha(data.frame(flipScale(tapsData$GIBRLAW1S30), flipScale(tapsData$GIBRLAW2S30), flipScale(tapsData$GIBRLAW4S30), flipScale(tapsData$GIBRLAW7S30), flipScale(tapsData$GIBRLAW8S30)))$score
tapsData$law.scalet0 <- alpha(data.frame(flipScale(tapsData$GIBRLAW1S6), flipScale(tapsData$GIBRLAW2S6), flipScale(tapsData$GIBRLAW4S6), flipScale(tapsData$GIBRLAW7S6), flipScale(tapsData$GIBRLAW8S6)))$score
tapsData$law.scalet1 <- alpha(data.frame(flipScale(tapsData$RLAW1S15), flipScale(tapsData$RLAW2S15), flipScale(tapsData$RLAW4S15), flipScale(tapsData$RLAW7S15), flipScale(tapsData$RLAW8S15)))$score

tapsData$male <- rep(0, nrow(tapsData))
tapsData$male[tapsData$gendersp == "Male"] <- 1
tapsData$male[tapsData$gendersp == "Refused"] <- NA
## Party
tapsData$pid <- as.numeric(tapsData$test_PID7)
tapsData$pid <- as.numeric(recode(tapsData$test_PID7, "'Strong Democrat'=7;'Not very strong Democrat' =6;'Lean Democrat' = 5 ;'Independent' = 4; 'Lean Republican' = 3; 'Not very strong Republican'=2; 'Strong Republican'=1"))
## Income
tapsData$income <- as.numeric(tapsData$incomehhldsp)
tapsData$income[tapsData$income == 1] <- NA
tapsData$income <- tapsData$income - 1
## White
tapsData$white <- rep(0, nrow(tapsData))
tapsData$white[tapsData$RACE1SP == "Yes"] <- 1
## Black
tapsData$black <- rep(0, nrow(tapsData))
tapsData$black[tapsData$RACE2SP == "Yes"] <- 1
## Latino
tapsData$hispanic <- rep(0, nrow(tapsData))
tapsData$hispanic[tapsData$HISPAN1SP == "Yes"] <- 1
## Obama Approval (Jan)
tapsData$approval.jan14 <- recode(as.numeric(tapsData$apprpressp), "1=NA;2=4;3=3;4=2;5=1;6=NA")
tapsData$approval.jan14.bin <- recode(as.numeric(tapsData$apprpressp), "1=NA;2=1;3=1;4=0;5=0;6=NA")
## Obama Approval (May)
tapsData$approval.may14 <- recode(as.numeric(tapsData$DEC13_JAN14_PRESAPPR), "1=NA;2=4;3=3;4=2;5=1;6=NA")
tapsData$approval.may14.bin <- recode(as.numeric(tapsData$DEC13_JAN14_PRESAPPR), "1=NA;2=1;3=1;4=0;5=0;6=NA")
## Obama Approval (Jan 2015)
tapsData$approval.jan15 <- recode(as.numeric(tapsData$APPRPRESS38), "1=NA;2=4;3=3;4=2;5=1;6=NA")
tapsData$approval.jan15.bin <- recode(as.numeric(tapsData$APPRPRESS38), "1=NA;2=1;3=1;4=0;5=0;6=NA")
## Ideology
tapsData$ideology <- recode(as.numeric(tapsData$IDEOL1SP), "1=NA;2=7;3=6;4=5;5=4;6=3;7=2;8=1;9=NA;10=NA")
## Education
tapsData$education <- as.numeric(tapsData$educsp)
tapsData$education[tapsData$education == 1] <- NA
tapsData$education <- tapsData$education-1
tapsData$education <- recode(tapsData$education, "1:9=1;10=2;11=3;12=4;13=5;14:16=6")
## Political knowledge questions
tapsData$correct1 <- rep(0, nrow(tapsData))
tapsData$correct1[tapsData$POLKNOW1S30 == "Republicans"] <- 1
tapsData$correct1[is.na(tapsData$POLKNOW1S30)] <- NA
tapsData$correct2 <- rep(0, nrow(tapsData))
tapsData$correct2[tapsData$POLKNOW2S30 == "a two-thirds majority of both houses of Congress"] <- 1
tapsData$correct2[is.na(tapsData$POLKNOW2S30)] <- NA
tapsData$correct3 <- rep(0, nrow(tapsData))
tapsData$correct3[tapsData$POLKNOW3S30 == "six years"] <- 1
tapsData$correct3[is.na(tapsData$POLKNOW3S30)] <- NA
tapsData$correct5 <- rep(0, nrow(tapsData))
tapsData$correct5[tapsData$POLKNOW5S30 == "a filibuster"] <- 1
tapsData$correct5[is.na(tapsData$POLKNOW5S30)] <- NA
tapsData$correct6 <- rep(0, nrow(tapsData))
tapsData$correct6[tapsData$POLKNOW6S30 == "Joseph Biden"] <- 1
tapsData$correct6[is.na(tapsData$POLKNOW6S30)] <- NA
tapsData$correct7 <- rep(0, nrow(tapsData))
tapsData$correct7[tapsData$POLKNOW7S30 == "two terms"] <- 1
tapsData$correct7[is.na(tapsData$POLKNOW7S30)] <- NA
tapsData$correct8 <- rep(0, nrow(tapsData))
tapsData$correct8[tapsData$POLKNOW12S30 == "life terms"] <- 1
tapsData$correct8[is.na(tapsData$POLKNOW12S30)] <- NA
tapsData$correct9 <- rep(0, nrow(tapsData))
tapsData$correct9[tapsData$POLKNOW13S30 == "John Roberts"] <- 1
tapsData$correct9[is.na(tapsData$POLKNOW13S30)] <- NA
tapsData$correct10 <- rep(0, nrow(tapsData))
tapsData$correct10[tapsData$POLKNOW17S30 == "the benefit program for senior citizens"] <- 1
tapsData$correct10[is.na(tapsData$POLKNOW17S30)] <- NA
tapsData$correct11 <- rep(0, nrow(tapsData))
tapsData$correct11[tapsData$POLKNOW18S30 == "Medicare"] <- 1
tapsData$correct11[is.na(tapsData$POLKNOW18S30)] <- NA
tapsData$correct12 <- rep(0, nrow(tapsData))
tapsData$correct12[tapsData$POLKNOW21S30 == "a federation"] <- 1
tapsData$correct12[is.na(tapsData$POLKNOW21S30)] <- NA
tapsData$correct13 <- rep(0, nrow(tapsData))
tapsData$correct13[tapsData$POLKNOW22S30 == "conservatives"] <- 1
tapsData$correct13[is.na(tapsData$POLKNOW22S30)] <- NA
tapsData$correct14 <- rep(0, nrow(tapsData))
tapsData$correct14[tapsData$POLKNOW23S30 == "President"] <- 1
tapsData$correct14[is.na(tapsData$POLKNOW23S30)] <- NA
tapsData$correct15 <- rep(0, nrow(tapsData))
tapsData$correct15[tapsData$POLKNOW24S30 == "Democrat"] <- 1
tapsData$correct15[is.na(tapsData$POLKNOW24S30)] <- NA
tapsData$correct16 <- rep(0, nrow(tapsData))
tapsData$correct16[tapsData$POLKNOW25S30 == "was originally called the Bureau of the Budget"] <- 1
tapsData$correct16[is.na(tapsData$POLKNOW25S30)] <- NA
tapsData$sum.correct <-   apply(tapsData[,c("correct1","correct2","correct3","correct5","correct6","correct7","correct8","correct9","correct10","correct11","correct12","correct13","correct14","correct15","correct16")], 1, sum)
tapsData$sum.correct.small <-   apply(tapsData[,c("correct1","correct2","correct6","correct9","correct13")], 1, sum)
cor(tapsData$sum.correct,tapsData$sum.correct.small,use="complete.obs")
library(psych)
tapsData$knowledge.scale <- 
  alpha(data.frame(tapsData$correct1,tapsData$correct2,tapsData$correct3,tapsData$correct5,tapsData$correct6,tapsData$correct7,tapsData$correct8,tapsData$correct9,tapsData$correct10,tapsData$correct11,tapsData$correct12,tapsData$correct13,tapsData$correct14,tapsData$correct15,tapsData$correct16))$score
detach("package:psych", unload=TRUE)
## Attitudes toward unilateral power
## Jan 14
tapsData$judges.jan14 <- recodeTapsPos(tapsData$REEVES6S25)
tapsData$judges.jan14.bin <- recode(tapsData$judges.jan, "1=0;2=0;3=1;4=1")
tapsData$agencies.jan14 <- recodeTapsPos(tapsData$REEVES7S25)
tapsData$agencies.jan14.bin <- recode(tapsData$agencies.jan, "1=0;2=0;3=1;4=1")
tapsData$execaction.jan14 <- recodeTapsPos(tapsData$REEVES8S25)
tapsData$execaction.jan14.bin <- recode(tapsData$execaction.jan, "1=0;2=0;3=1;4=1")
## May 14
tapsData$judges.may14 <- recodeTapsPos(tapsData$REEVES6S30)
tapsData$judges.may14.bin <- recode(tapsData$judges.may14, "1=0;2=0;3=1;4=1")
tapsData$agencies.may14 <- recodeTapsPos(tapsData$REEVES7S30)
tapsData$agencies.may14.bin <- recode(tapsData$agencies.may14, "1=0;2=0;3=1;4=1")
tapsData$execaction.may14 <- recodeTapsPos(tapsData$REEVES8S30)
tapsData$execaction.may14.bin <- recode(tapsData$execaction.may14, "1=0;2=0;3=1;4=1")
## Jan 15
tapsData$judges.jan15 <- recodeTapsPos(tapsData$REEVES6S25)
tapsData$judges.jan15.bin <- recode(tapsData$judges.jan15, "1=0;2=0;3=1;4=1")
tapsData$agencies.jan15 <- recodeTapsPos(tapsData$REEVES7S38)
tapsData$agencies.jan15.bin <- recode(tapsData$agencies.jan15, "1=0;2=0;3=1;4=1")
tapsData$execaction.jan15 <- recodeTapsPos(tapsData$REEVES8S38)
tapsData$execaction.jan15.bin <- recode(tapsData$execaction.jan15, "1=0;2=0;3=1;4=1")
## TAPS Jan 14
## Unilateral policymaking
## column 2 of SI Table 1
tapsj.model.cong <- glm(execaction.jan14.bin ~ law.scale + approval.jan14  + ideology, 
                        data = tapsData, 
                        family = binomial(link = "logit"),
                        weights = jan2014wt1)
## column 3 of SI Table 1
tapsm.model.cong <- glm(execaction.may14.bin ~ law.scale + approval.may14  + ideology, 
                        data = tapsData, 
                        family = binomial(link = "logit"),
                        weights = may2014wt1)
## column 4 of SI Table 1
tapsj2.model.cong <- glm(execaction.jan15.bin ~ law.scale + approval.jan15  + ideology, 
                         data = tapsData, 
                         family = binomial(link = "logit"),
                         weights = jan2015wt1)
## Judicial Appointments
## column 2 of SI Table 2
tapsj.model.judges <- glm(judges.jan14.bin ~ law.scale + approval.jan14  + ideology, 
                        data = tapsData, 
                        family = binomial(link = "logit"),
                        weights = jan2014wt1)
## column 3 of SI Table 2
tapsm.model.judges <- glm(judges.may14.bin ~ law.scale + approval.may14  + ideology, 
                        data = tapsData, 
                        family = binomial(link = "logit"),
                        weights = may2014wt1)
## column 4 of SI Table 2
tapsj2.model.judges <- glm(judges.jan15.bin ~ law.scale + approval.jan15  + ideology, 
                         data = tapsData, 
                         family = binomial(link = "logit"),
                         weights = jan2015wt1)
## Bureaucratic Implementation
## column 2 of SI Table 3
tapsj.model.agencies <- glm(agencies.jan14.bin ~ law.scale + approval.jan14  + ideology, 
                          data = tapsData, 
                          family = binomial(link = "logit"),
                          weights = jan2014wt1)
## column 3 of SI Table 3
tapsm.model.agencies <- glm(agencies.may14.bin ~ law.scale + approval.may14  + ideology, 
                          data = tapsData, 
                          family = binomial(link = "logit"),
                          weights = may2014wt1)
## column 4 of SI Table 3
tapsj2.model.agencies <- glm(agencies.jan15.bin ~ law.scale + approval.jan15  + ideology, 
                           data = tapsData, 
                           family = binomial(link = "logit"),
                           weights = jan2015wt1)

## Replication for Table 5 in the Online Appendix
##Jan 15
ej15 <- glm(execaction.jan15.bin ~ law.scale + approval.jan15  + ideology + knowledge.scale, 
                data = tapsData, 
                family = binomial(link = "logit"),
                weights = jan2015wt1)
jj15 <- glm(judges.jan15.bin ~ law.scale + approval.jan15  + ideology + knowledge.scale, 
           data = tapsData, 
           family = binomial(link = "logit"),
           weights = jan2015wt1)
aj15 <- glm(agencies.jan15.bin ~ law.scale + approval.jan15  + ideology + knowledge.scale, 
           data = tapsData, 
           family = binomial(link = "logit"),
           weights = jan2015wt1)

##Jan 14
ej14 <- glm(execaction.jan14.bin ~ law.scale + approval.jan14  + ideology + knowledge.scale, 
           data = tapsData, 
           family = binomial(link = "logit"),
           weights = jan2014wt1)
jj14 <- glm(judges.jan14.bin ~ law.scale + approval.jan14  + ideology + knowledge.scale, 
           data = tapsData, 
           family = binomial(link = "logit"),
           weights = jan2014wt1)
aj14 <- glm(agencies.jan14.bin ~ law.scale + approval.jan14  + ideology + knowledge.scale, 
           data = tapsData, 
           family = binomial(link = "logit"),
           weights = jan2014wt1)

##May 14
em14 <- glm(execaction.may14.bin ~ law.scale + approval.may14  + ideology + knowledge.scale, 
           data = tapsData, 
           family = binomial(link = "logit"),
           weights = may2014wt1)
jm14 <- glm(judges.may14.bin ~ law.scale + approval.may14  + ideology + knowledge.scale, 
           data = tapsData, 
           family = binomial(link = "logit"),
           weights = may2014wt1)
am14 <- glm(agencies.may14.bin ~ law.scale + approval.may14  + ideology + knowledge.scale, 
           data = tapsData, 
           family = binomial(link = "logit"),
           weights = may2014wt1)

names(ej14$coefficients)[names(ej14$coefficients)=="approval.jan14"] <- "approval"
names(em14$coefficients)[names(em14$coefficients)=="approval.may14"] <- "approval"
names(ej15$coefficients)[names(ej15$coefficients)=="approval.jan15"] <- "approval"

names(jj14$coefficients)[names(jj14$coefficients)=="approval.jan14"] <- "approval"
names(jm14$coefficients)[names(jm14$coefficients)=="approval.may14"] <- "approval"
names(jj15$coefficients)[names(jj15$coefficients)=="approval.jan15"] <- "approval"

names(aj14$coefficients)[names(aj14$coefficients)=="approval.jan14"] <- "approval"
names(am14$coefficients)[names(am14$coefficients)=="approval.may14"] <- "approval"
names(aj15$coefficients)[names(aj15$coefficients)=="approval.jan15"] <- "approval"



apsrtable(ej14, em14, ej15, jj14, jm14, jj15, aj14, am14, aj15, 
          stars = "default",
          model.names = c("Jan 14", "May 14", "Jan 15", "Jan 14", "May 14", "Jan 15", "Jan 14", "May 14", "Jan 15"),
          coef.names = c("Intercept", "Belief in rule of law", "Presidential Approval", 
                         "Ideology", "Political Knowledge"), 
          caption.position = "below",
          caption = c("\\textbf{The Role of Political Knowledge in Models of Support for Unilateral Policymaking, Judicial Appointments, and Bureaucratic Implementation.}" )
          )
